package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AreaBiz;
import com.useeinfo.oa.modules.base.entity.Area;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/area")
public class AreaAPI {
    @Autowired
    private AreaBiz areaBiz;

    /**
     * 获取区域列表
     */
    @RequestMapping(value = "/getAreaList")
    public ResultDto getAreaList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                 @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                 @RequestParam(value = "ancestryId", required = false) String ancestryIdParam,
                                 @RequestParam(value = "parentId", required = false) String parentIdParam,
                                 HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        queryParam.setSqlOrder(" order by s.lvl asc ");
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        if(StringUtils.isNotBlank(ancestryIdParam)){
            queryParam.getSqlMap().put("ancestryId", ancestryIdParam);
        }
        if(StringUtils.isNotBlank(parentIdParam)){
            queryParam.getSqlMap().put("parentId", parentIdParam);
        }
        List<Area> areaList =areaBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (Area area : areaList){
            JSONObject json = new JSONObject();
            json.put("areaName",area.getAreaName());
            json.put("areaCode",area.getAreaCode());
            json.put("id",area.getId());
            array.add(json);
        }
        return ResultUtil.success(array);
    }

    /**
     * 获取区域完全展示json
     */
    @RequestMapping(value = "/getAreaTreeData")
    public ResultDto getAreaTreeData(@RequestParam(value = "parentId", required = false) String parentIdParam,
                                      HttpServletRequest request){
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        JSONArray array = areaBiz.findTreeData(StringConverters.ToLong(parentIdParam),enterprise!=null?enterprise.getId():null);
        return  ResultUtil.success(array);
    }
}
